using System;
using System.Collections.Generic;
using System.Linq;
using data.contracts;
using modelbuilder.parser.pins;

namespace modelbuilder.parts
{
    public class Parse_Pins : IParse_pins
    {
        public event Action<IEnumerable<Board>> Result;

        public void Process(IEnumerable<Board> boards) {
            var result = new List<Board>();
            foreach (var board in boards) {
                ParsePins(board);
                result.Add(board);
            }
            Result(result);
        }

        private static void ParsePins(Board board) {
            foreach (var wire in board.Wires) {
                ParsePins(wire);
            }
        }

        private static void ParsePins(Wire wire) {
            var fromPins = new EbcPinParser().Parse(wire.From);
            wire.FromPin = fromPins.First();
            var toPins = new EbcPinParser().Parse(wire.To);
            wire.ToPin = toPins.First();
        }
    }
}